//
//  LeaderView.m
//  specialine
//
//  Created by 往返网络 on 17/2/10.
//  Copyright © 2017年 com. All rights reserved.
//

#import "LeaderView.h"

@interface LeaderView ()<UIScrollViewDelegate>

@property (nonatomic,strong) dispatch_source_t timer; //定时器

@end

@implementation LeaderView
{
    
    UIScrollView * scrollview;
    
    UIButton * testnow; //立即体验
    
    UIPageControl * pagecontr;
    
    UILabel * firstlab;
    
    UILabel * secondlab;
    
    UILabel * thirdlab;
    
    
}

- (instancetype)initWithFrame:(CGRect)frame{


    if (self = [super initWithFrame:frame]) {
        
        [self createUI];
        
    }

    return self;

}


- (void)createUI{

    
    scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
    
    scrollview.contentSize = CGSizeMake(SCREEN_W * 3, SCREEN_H);
    
    scrollview.backgroundColor = [UIColor whiteColor];
    
    scrollview.scrollEnabled = NO;
    
    [self addSubview:scrollview];
    
    
    for (int i = 0; i < 3; i ++ ) {
        
        UIImageView * imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i*SCREEN_W, 0, SCREEN_W, SCREEN_H)];
        
        imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"LeaderImage%d.png",i]];
        
        [scrollview addSubview:imageview];
        
        scrollview.pagingEnabled = YES;
        
        scrollview.showsVerticalScrollIndicator = NO;
        
        scrollview.showsVerticalScrollIndicator = NO;
        
        
    }
    
    UIView * labback = [[UIView alloc]initWithFrame:CGRectMake(SCREEN_W*0.5 - 90.0/W_BASE*SCREEN_W, SCREEN_H - 180.0/H_BASE*SCREEN_H, 180.0/W_BASE*SCREEN_W, 50.0/H_BASE*SCREEN_H)];
    
    [self addSubview:labback];
    
    
    firstlab = [[UILabel alloc]initWithFrame:CGRectMake(20.25/W_BASE*SCREEN_W, 21.0/H_BASE*SCREEN_H, 33.0/W_BASE*SCREEN_W, 8.0/H_BASE*SCREEN_H)];
    
    firstlab.layer.cornerRadius = 2;
    
    firstlab.clipsToBounds = YES;
    
    firstlab.backgroundColor = [UIColor whiteColor];

    [labback addSubview:firstlab];
    
    
    secondlab = [[UILabel alloc]initWithFrame:CGRectMake(20.25*2/W_BASE*SCREEN_W + 33.0/W_BASE*SCREEN_W, 21.0/H_BASE*SCREEN_H, 33.0/W_BASE*SCREEN_W, 8.0/H_BASE*SCREEN_H)];
    
    secondlab.layer.cornerRadius = 2;
    
    secondlab.clipsToBounds = YES;
    
    [labback addSubview:secondlab];
    
    secondlab.backgroundColor = COLORRGBA(156, 187, 253, 1);

    
    thirdlab = [[UILabel alloc]initWithFrame:CGRectMake(20.25*3/W_BASE*SCREEN_W + 33.0/W_BASE*SCREEN_W*2, 21.0/H_BASE*SCREEN_H, 33.0/W_BASE*SCREEN_W, 8.0/H_BASE*SCREEN_H)];
    
    thirdlab.layer.cornerRadius = 2;
    
    thirdlab.clipsToBounds = YES;
    
    [labback addSubview:thirdlab];
    
    thirdlab.backgroundColor = COLORRGBA(156, 187, 253, 1);
    
    
    
    //启动定时器
    [self loadTimer];
    


}

#pragma makrk -----------进入登陆界面 - -- -- - - -- - - - --
- (void)isTestNow{


    [self.leaderviewDelegate testNow];
    


}

#pragma mark - - - - － － － －启动定时器 － － －－ － －－ － －－ － －－
- (void)loadTimer{
    
    __block NSInteger time = 3; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //进入登陆界面
                
                
                //前面加入启动页面
                
                [self.leaderviewDelegate testNow];
                
                
            });
            
        }else{
            
            
            
            NSInteger seconds = time ;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置按钮显示读秒效果
                
                scrollview.contentOffset = CGPointMake(SCREEN_W * (seconds-1), 0);
                
                NSLog(@"second is %ld",seconds);
                
                
                if (seconds == 2) {
                    
                    firstlab.backgroundColor = COLORRGBA(156, 187, 253, 1);

                    secondlab.backgroundColor = [UIColor whiteColor];
                    
                    thirdlab.backgroundColor = COLORRGBA(156, 187, 253, 1);
                    
                    
                }else if (seconds == 1)
                    
                {
                
                    firstlab.backgroundColor = COLORRGBA(156, 187, 253, 1);
                    
                    secondlab.backgroundColor = COLORRGBA(156, 187, 253, 1);

                    thirdlab.backgroundColor  = [UIColor whiteColor];
                    
                }
                
                
            });
            
            time--;
            
        }
    });
    
    dispatch_resume(_timer);
    
}

#pragma mark - -- -- - - - -- - - -界面消失时，销毁定时器 - -- - -- - - - -- - -

- (void)dealloc{

  //  终止定时器
    
        if (_timer) {
    
            dispatch_source_cancel(_timer);
            
        }

}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/






@end
